package com.example.uniappspringbootstartweb.controller;

import com.example.uniappspringbootstartweb.entity.Content;
import com.example.uniappspringbootstartweb.entity.Publish;
import com.example.uniappspringbootstartweb.entity.Userlike;
import com.example.uniappspringbootstartweb.service.ContentService;
import com.example.uniappspringbootstartweb.service.PublishService;
import com.example.uniappspringbootstartweb.service.UserlikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by lilijava
 * on 2024/12/30
 * <p>
 * on  uni-app-springboot-start-web
 * @author DELL
 */
@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/api/publish")
public class PublishController {
    @Autowired
    PublishService publishService;
    @Autowired
    ContentService contentService;
    @GetMapping ("/")
    public ResponseEntity<List<Content>>getLikeId(Integer userid) {

        List<Publish> publishes = publishService.findAllByUserid((Long.valueOf(userid)));
        // 创建一个列表来存储获取到的内容
        List<Content> contents = new ArrayList<>();
        // 遍历每个 Userlike 对象并获取对应的内容
        for (Publish publishe : publishes) {
            Long contentId = publishe.getContentid();
            Optional<Content> optionalContent = Optional.ofNullable(contentService.getContentById(contentId));

            // 如果找到了内容，则添加到列表中
            optionalContent.ifPresent(contents::add);
        }
        return  ResponseEntity.ok(contents);
    }
}
